#include <iostream>
#include <memory>
#include <string>
#include "mysqlConn.h"
#include <thread>
#include "connectionpool.h"

int query()
{
    MySqlConn conn;
    conn.connent("root", "123456", "test_db", "127.0.0.1", 8888);
    std::string sql = "insert into student(name,age) values('zengkuyi',18)";
    bool flag = conn.update(sql);
    if (flag == true)
    {
        std::cout << "insert success" << std::endl;
    }
    else
    {
        std::cout << "insert fail" << std::endl;
    }

    sql = "select * from student";
    flag = conn.query(sql);

    if (flag == true)
    {
        std::cout << "insert success" << std::endl;
    }
    else
    {
        std::cout << "insert fail" << std::endl;
    }

    while (conn.next())
    {
        std::cout << conn.value(0) << " " << conn.value(1) << std::endl;
    }

    return 0;
}

void op1(int begin, int end)
{
    for (int i = begin; i < end; ++i)
    {
        MySqlConn conn;
        conn.connent("root", "123456", "test_db", "127.0.0.1", 8888);
        char sql[1024] = {0};
        sprintf(sql, "insert into student values('man', 18)");
        conn.update(sql);
    }
}

void op2(ConnectionPool *pool, int begin, int end)
{
    for (int i = begin; i < end; ++i)
    {
        shared_ptr<MySqlConn> conn = pool->getConnection();
        char sql[1024] = {0};
        sprintf(sql, "insert into student values('man', 18)");
        conn->update(sql);
    }
}
void test1()
{
#if 1
    steady_clock::time_point begin = steady_clock::now();
    op1(0, 5000);
    steady_clock::time_point end = steady_clock::now();
    auto length = end - begin;
    cout << "非连接池，单线程，用时: " << length.count() << " 纳秒，"
         << length.count() / 1000000 << " 毫秒" << endl;
#else
    ConnectionPool *pool = ConnectionPool::getConnectionPool();
    steady_clock::time_point begin = steady_clock::now();
    op2(pool, 0, 5000);
    steady_clock::time_point end = steady_clock::now();
    auto length = end - begin;
    cout << "连接池，单线程，用时: " << length.count() << " 纳秒，"
         << length.count() / 1000000 << " 毫秒" << endl;
#endif
}

void test2()
{
#if 0
// 127491 
// 解决方法额外进行一次连接
    MySqlConn conn;
    conn.connent("root", "123456", "test_db", "127.0.0.1", 8888);
// mysql 若一段时间发现全是相同的用户名密码 会拒绝一些连接
    steady_clock::time_point begin = steady_clock::now();
    thread t1(op1, 0, 1000);
    thread t2(op1, 1000, 2000);
    thread t3(op1, 2000, 3000);
    thread t4(op1, 3000, 4000);
    thread t5(op1, 4000, 5000);
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();
    steady_clock::time_point end = steady_clock::now();
    auto length = end - begin;
    cout << "非连接池，多线程，用时: " << length.count() << " 纳秒，"
         << length.count() / 1000000 << " 毫秒" << endl;
#else
    ConnectionPool *pool = ConnectionPool::getConnectionPool();
    steady_clock::time_point begin = steady_clock::now();
    thread t1(op2,pool, 0, 1000);
    thread t2(op2,pool, 1000, 2000);
    thread t3(op2,pool, 2000, 3000);
    thread t4(op2,pool, 3000, 4000);
    thread t5(op2,pool, 4000, 5000);
    t1.join();
    t2.join();
    t3.join();
    t4.join();
    t5.join();
    steady_clock::time_point end = steady_clock::now();
    auto length = end - begin;
    cout << "连接池，多线程，用时: " << length.count() << " 纳秒，"<< length.count() / 1000000 << " 毫秒" << endl;
#endif
}

int main()
{
    test2();
    return 0;
}